package com.net.oil.util;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.net.oil.app.MyApp;

/**
 * 作者：lch
 * 时间：2019/1/27 0027:下午 15:14
 * 邮箱：245850413@qq.com
 * 说明：
 */
public class AppInfo {

    /**
     * 获取版本号
     *
     * @return 版本号
     */
    public static String getVersionName() {
        PackageManager packageManager = MyApp.getApplication().getPackageManager();
        if (packageManager != null) {
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(MyApp.getApplication().getPackageName(), 0);
                if (packageInfo != null && !TextUtils.isEmpty(packageInfo.versionName)) {
                    return packageInfo.versionName;     //获取版本号
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return "1.0.0";         //出现异常则返回1.0.0
    }

    /**
     * 获取版本code
     *
     * @return code
     */
    public static int getVersionCode() {
        int versionCode = -1;
        PackageManager manager = MyApp.getApplication().getPackageManager();
        try {
            PackageInfo packageInfo = manager.getPackageInfo(MyApp.getApplication().getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }
}
